<?php

namespace addons\facrm\controller\facrm;

use addons\facrm\library\BackendApi;



/**
 * 操作记录
 */
class Operatelog extends BackendApi
{

    protected $model = null;
    protected $noNeedRight = ['index'];
    /**
     * 快速搜索时执行查找的字段
     */
    protected $searchFields = 'content';

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('\app\admin\model\facrm\Operatelog');
        $this->request->filter(['strip_tags']);
    }

    /**
     * 查看
     * @ApiSummary (参数根据实际需要传，不需要三个都一起传)
     * @ApiMethod (GET)
     * @ApiParams (name="customer_id", type="int", required=false, description="客户ID")
     * @ApiParams (name="contract_id", type="int", required=false, description="合同ID")
     * @ApiParams (name="receivable_id", type="int", required=false, description="回款ID")
     * @ApiBody ("有分页")
     */
    public function index($customer_id = null, $contract_id = null, $receivable_id = null)
    {
        $customer_id = $customer_id ? $customer_id : $this->request->param("customer_id");
        $contract_id = $contract_id ? $contract_id : $this->request->param("contract_id");
        $receivable_id = $receivable_id ? $receivable_id : $this->request->param("receivable_id");


        $filter_w = [];
        if ($customer_id) {
            $filter_w['customer_id'] = $customer_id;
        }
        if ($contract_id) {
            $filter_w['contract_id'] = $contract_id;
        }
        if ($receivable_id) {
            $filter_w['receivable_id'] = $receivable_id;
        }
        list($where, $sort, $order, $offset, $limit) = $this->buildparams();

        $total = $this->model
            ->where($where)
            ->where($filter_w)
            ->order($sort, $order)->fetchSql(false)
            ->count();
        $list = $this->model
            ->where($filter_w)
            ->where($where)
            ->order($sort, $order)
            ->limit($offset, $limit)->fetchSql(false)
            ->select();
        foreach ($list as $row) {
            $row->hidden(['content']);
        }
        $result = array("total" => $total, "rows" => $list);
        return $this->success('', $result);
    }


    /**
     * 日志详情
     * @ApiParams (name="ids", type="int", required=true, description="日志ID")
     */
    public function detail($ids = NULL)
    {
        $ids = $ids ? $ids : $this->request->param("ids");
        $row = $this->model->get(['id' => $ids]);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        return $this->success('', $row->toArray());
    }


}
